package com.heima.behavior.Service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.heima.behavior.Service.BehaviorService;
import com.heima.behavior.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 保存关注行为
     * @param dto
     * @return
     */
    @Override
    public void saveFollow(FollowBehaviorDto dto) {
        if (ObjectUtil.isEmpty(dto)){
            throw new RuntimeException("数据异常！");
        }
        // 1、判断是否新增还是修改
        boolean exists = mongoTemplate.exists(Query.query(Criteria
                .where("authorId").is(dto.getAuthorId())), FollowBehaviorDto.class);
        // 存在->删除
        if (exists){
            mongoTemplate.remove(Query.query(Criteria
                            .where("authorId").is(dto.getAuthorId()))
                    ,FollowBehaviorDto.class);
        }else {
            mongoTemplate.save(dto);
        }

    }

    /**
     * 保存点赞行为
     * @param dto
     * @return
     */
    @Override
    public void saveLikes(LikesBehaviorDto dto) {
        if (ObjectUtil.isEmpty(dto)){
            throw new RuntimeException("数据异常！");
        }
        // 1、判断是否新增还是修改
        boolean exists = mongoTemplate.exists(Query.query(Criteria
                .where("articleId").is(dto.getArticleId()))
                , LikesBehaviorDto.class);
        // 存在，修改
        if (exists){
            mongoTemplate.remove(Query.query(Criteria
                            .where("articleId").is(dto.getArticleId()))
                    ,LikesBehaviorDto.class);

        }else {
            mongoTemplate.save(dto);
        }
    }

    /**
     * 阅读行为
     * @param dto
     * @return
     */
    @Override
    public void saveRead(ReadBehaviorDto dto) {
        if (ObjectUtil.isEmpty(dto)){
            throw new RuntimeException("数据异常！");
        }
        // 1、判断是否新增还是修改
        ReadBehaviorDto articleId = mongoTemplate.findOne(Query.query(Criteria
                        .where("articleId").is(dto.getArticleId()))
                , ReadBehaviorDto.class);
        // 存在->删除
        if (ObjectUtil.isNotEmpty(articleId)){
            mongoTemplate.updateFirst(Query.query(Criteria
                            .where("articleId").is(dto.getArticleId()))
                    , new Update().set("count",articleId.getCount()+1)
                    ,ReadBehaviorDto.class);
        }else {
            mongoTemplate.save(dto);
        }
    }

    /**
     * 保存不喜欢行为
     * @param dto
     * @return
     */
    @Override
    public void saveUnLikes(UnLikesBehaviorDto dto) {
        if (ObjectUtil.isEmpty(dto)){
            throw new RuntimeException("数据异常！");
        }
        // 1、判断是否新增还是修改
        boolean exists = mongoTemplate.exists(Query.query(Criteria
                        .where("articleId").is(dto.getArticleId()))
                , UnLikesBehaviorDto.class);
        // 存在->删除
        if (exists){
            mongoTemplate.remove(Query.query(Criteria
                            .where("articleId").is(dto.getArticleId()))
                    ,UnLikesBehaviorDto.class);

        }else {
            mongoTemplate.save(dto);
        }
    }

    /**
     * 保存收藏行为
     * @param dto
     * @return
     */
    @Override
    public void saveCollection(CollectionBehaviorDto dto) {
        if (ObjectUtil.isEmpty(dto)){
            throw new RuntimeException("数据异常！");
        }
        // 1、判断是否新增还是修改
        boolean exists = mongoTemplate.exists(Query.query(Criteria
                        .where("articleId").is(dto.getArticleId()))
                , CollectionBehaviorDto.class);
        // 存在->删除
        if (exists){
            mongoTemplate.remove(Query.query(Criteria
                            .where("articleId").is(dto.getArticleId()))
                    ,CollectionBehaviorDto.class);

        }else {
            mongoTemplate.save(dto);
        }
    }

    /**
     * 状态回显
     * @param dto
     * @return
     */
    @Override
    public HashMap<String, Object> loadArticleBehavior(ArticleBehaviorDto dto) {
        HashMap<String, Object> data = new HashMap<>();
        boolean islike =  mongoTemplate.exists(Query.query(Criteria
                        .where("articleId").is(dto.getArticleId()))
                , LikesBehaviorDto.class);
        boolean isunlike = mongoTemplate.exists(Query.query(Criteria
                        .where("articleId").is(dto.getArticleId()))
                , UnLikesBehaviorDto.class);
        boolean iscollection = mongoTemplate.exists(Query.query(Criteria
                        .where("articleId").is(dto.getArticleId()))
                , CollectionBehaviorDto.class);
        boolean isfollow = mongoTemplate.exists(Query.query(Criteria
                        .where("authorId").is(dto.getAuthorId()))
                , FollowBehaviorDto.class);
        data.put("islike",islike);
        data.put("isunlike",isunlike);
        data.put("isfollow",isfollow);
        data.put("iscollection",iscollection);
        return data;
    }
}
